package com.xju.atpgs.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

public class FileUtil {
    public final static String IMG_PATH_PREFIX="upload/img";
    public final static String FILE_PATH_PREFIX="upload/file";
    public static File getImgDirFile(){
        String FileDirPath=new String("D:/"+IMG_PATH_PREFIX);
        File fileDir=new File(FileDirPath);
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }
        return fileDir;
    }
    public static File getExcelDirFile(){
        String FileDirPath=new String("D:/"+FILE_PATH_PREFIX);
        File fileDir=new File(FileDirPath);
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }
        return fileDir;
    }
    public static void uploadImg(MultipartFile file) throws IOException {
        if (file!=null){
            File fileDir= FileUtil.getImgDirFile();
            String filename=file.getOriginalFilename();
            File newfile=new File(fileDir.getAbsolutePath()+File.separator+filename);
            file.transferTo(newfile);
        }
    }
    public static void uploadFile(MultipartFile file) throws IOException {
        if (file!=null){
            File fileDir= FileUtil.getExcelDirFile();
            String filename=file.getOriginalFilename();
            File newfile=new File(fileDir.getAbsolutePath()+File.separator+filename);
            file.transferTo(newfile);
        }
    }
}
